package com.hydl.power.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.*;
import com.hydl.power.pojo.*;
import com.hydl.power.service.WorkOrderService;
import com.hydl.power.service.WorkOrderService;
import com.hydl.power.utils.CalculateDistanceUtils;
import com.hydl.power.utils.IdUtil;
import com.hydl.power.utils.MessageUtils;
import com.hydl.power.utils.SmsUtils;
import org.apache.commons.lang3.StringUtils;
import org.gavaghan.geodesy.Ellipsoid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wxb
 * @date 2020/3/31 15:11
 */
@Service
public class WorkOrderServiceImpl implements WorkOrderService {

    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Autowired
    private OrderDeliveryMenuMapper orderDeliveryMenuMapper;

    @Autowired
    private MaintainerMapper maintainerMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private OrderDeliveryMapper orderDeliveryMapper;


    @Override
    public PageInfo<WorkOrder> getAllWorkOrderByList(Integer pageNum, Integer pageSize, Long beginTime, Long endTime, Integer orderTypes, Integer rank, Integer orderStatus) {
        List<WorkOrder> WorkOrderList = workOrderMapper.getAllWorkOrderByList(pageNum, pageSize, beginTime, endTime, orderTypes, rank,orderStatus);
        return new PageInfo<>(WorkOrderList);
    }



    @Override
    public PageInfo<WorkOrder> getAllWorkOrderTwo(Integer pageNum, Integer pageSize, String begin, String end, Integer orderStatus, Integer maintainerId, String areaId, String projectId, String roomId) {
        List<WorkOrder>WorkOrderList = workOrderMapper.getAllWorkOrderTwo(pageNum, pageSize, begin, end, orderStatus,maintainerId, areaId, projectId, roomId);
        return new PageInfo<>(WorkOrderList);
    }

    @Override
    public PageInfo<WorkOrder> getIncidentRecord(Integer pageNum, Integer pageSize, Long begin, Long end, Integer rank, Integer faultTypes, Integer orderStatus, String roomName, String deviceName, String deviceAddress,String token) {
        if(StringUtils.isNotBlank(token)){
            SysUser user = userMapper.getUserByToken(token);
            if(user!=null){
                if(user.getStatus()==2){
                    List<WorkOrder> workOrderList = workOrderMapper.getIncidentRecord(pageNum, pageSize, begin, end, rank, faultTypes, orderStatus, roomName, deviceName, deviceAddress,user.getProjectId());
                    return new PageInfo<>(workOrderList);
                }else{
                    List<WorkOrder> workOrderList = workOrderMapper.getIncidentRecord(pageNum, pageSize, begin, end, rank, faultTypes, orderStatus, roomName, deviceName, deviceAddress,null);
                    return new PageInfo<>(workOrderList);
                }
            }
        }
        return null;
    }

    @Override
    public List<OrderDeliveryMenu> getAllByPId() {
        return orderDeliveryMenuMapper.getAllByPId();
    }

    @Override
    public List<OrderDeliveryMenu> getAllByPIds(String pId) {
        return orderDeliveryMenuMapper.getAllByPId1(pId);
    }

    @Override
    public List<WorkOrder> getList() {
        return workOrderMapper.getList();
    }

    @Override
    public int updateOrderStatusById(Integer id, Integer orderStatus) {
        return workOrderMapper.updateOrderStatus(id,orderStatus);
    }

    @Override
    public WorkOrder getById(Integer id) {
        return workOrderMapper.selectById(id);
    }




    @Override
    public ArrayList<OrderDeliveryMenu> getTree() {
        List<OrderDeliveryMenu> list = orderDeliveryMenuMapper.getAll();
        ArrayList<OrderDeliveryMenu> WorkOrderMenuList = new ArrayList<>();
        for (OrderDeliveryMenu WorkOrderMenu: list) {
            if(WorkOrderMenu.getpName() == null ){
                WorkOrderMenuList.add(WorkOrderMenu);
            }
        }
        for (OrderDeliveryMenu WorkOrderMenu: WorkOrderMenuList) {
            WorkOrderMenu.setChilds(getChild(WorkOrderMenu.getNavId(),list) );
        }
        return WorkOrderMenuList;
    }


    @Override
    public List<Maintainer> getMaintainer(String powerroomId) {
        return maintainerMapper.getMaintainer(powerroomId);
    }

    @Override
    public int updateMaintainer(String maintainerId,String workOrderId,String projectName,String roomName,String deviceName,String phone){
        WorkOrder workOrder = workOrderMapper.WorkOrderId(workOrderId);
        workOrder.setMaintainerId(Integer.parseInt(maintainerId));
        Maintainer maintainer = maintainerMapper.selectById1(maintainerId);
        workOrder.setMaintainerName(maintainer.getName());
        workOrder.setDealBeginTime(new Date());
        workOrder.setOrderDeliveryTime(new Date());
        workOrder.setPhone(phone);
        //发送短信
        Map<String, String> map = new HashMap<String, String>();
        map.put("projectName", workOrder.getProjectName());
        map.put("powerRoomName", workOrder.getRoomName());
        map.put("deviceName", workOrder.getDeviceName());
        map.put("fault", workOrder.getFault());
        String data = JSONObject.toJSONString(map);
        SmsUtils.sendSms(workOrder.getPhone(),"合一电力","SMS_196616561",data);
        workOrderMapper.updateWorkOrderById(workOrder);
        orderDeliveryMapper.updateOrderStatusById(3,null,null,null,workOrder.getDealBeginTime(),Integer.valueOf(workOrderId));
        return workOrderMapper.updateMaintainer(workOrder);
    }


    private ArrayList<OrderDeliveryMenu> getChild(String pid, List<OrderDeliveryMenu> workOrderMenuList){
        ArrayList<OrderDeliveryMenu> childs = new ArrayList<>();
        for (OrderDeliveryMenu workOrderMenu: workOrderMenuList) {
            if(workOrderMenu.getpName() != null){
                if(workOrderMenu.getpId().equals(pid)){
                    childs.add(workOrderMenu);
                }
            }
        }
        for (OrderDeliveryMenu workOrderMenu: childs) {
            workOrderMenu.setChilds(getChild(workOrderMenu.getNavId(), workOrderMenuList));
        }
        if( childs.size() == 0 ){
            return null;
        }
        return childs;
    }

    @Override
    public List<OpsReport> queryByDeviceId(String deviceId) {
        List<OpsReport> list = workOrderMapper.queryByDeviceId(deviceId);
        String pattern = "EEE MMM dd HH:mm:ss zzz yyyy";
        SimpleDateFormat df = new SimpleDateFormat(pattern, Locale.US);
        String faultTypes="";
        for (OpsReport opsReport : list) {
            faultTypes=opsReport.getFaultTypes();
            switch (faultTypes){
                case "1":
                    faultTypes="越限告警";
                    break;
                case "2" :
                    faultTypes="变位告警";
                    break;
                case "3" :
                    faultTypes="电量反转";
                    break;
                case "4" :
                    faultTypes="时间反转";
                    break;
                case "5" :
                    faultTypes="数据异常";
                    break;
                case "6" :
                    faultTypes="通信状态告警";
                    break;
            }
            opsReport.setFaultTypes(faultTypes);
            /*if(workOrder.getDealBeginTime()!=null){
                try {
                    //时间格式转换
                    Date date = df.parse(workOrder.getDealBeginTime().toString());
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String dateString = simpleDateFormat.format(date);
                    System.out.println(dateString);
                    //map.put("deaBeginTime",dateString);
                }
                catch (ParseException e) {
                    e.printStackTrace();
                }
            }*/
        }
        return list;
    }

    @Override
    public List<WorkOrder> getWorkOrderByProjectId(String projectId) {
        return workOrderMapper.getWorkOrderByProjectId(projectId);
    }

    @Override
    public WorkOrder getWorkOrderByOrderDeliveryId(String orderDeliveryId) {
        return workOrderMapper.getWorkOrderByOrderDeliveryId(orderDeliveryId);
    }
}


